package com.ftg.learn.day10;

import javax.swing.*;
import java.util.Objects;
import java.util.Stack;

/**
 * 练习
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/8/7 上午8:08
 */
public class HomeWork01 {

    /**
     * 解决 正数以及负数的反置问题
     *
     * @param x 正整数
     * @return
     */
    public static int intReverse(long x) {
        boolean isfu = false;
        if (x < 0) {
            x = Math.abs(x);
            isfu = true;
        }

        if (x > Integer.MAX_VALUE) {
            throw new RuntimeException("超过");
        }

        long sum = 0;
        while (x > 0) {
            long ge = x % 10;
            sum = sum * 10 + ge;
            x /= 10;
        }

        if (sum > Integer.MAX_VALUE) {
            throw new RuntimeException("超过");
        }

        return isfu ? new Long(-sum).intValue() : (int) sum;
    }

    public static boolean question02(String str) {
        //0 ( 1 )
        int[] arr = new int[6];
        for (int i = 0; i < str.length(); i++) {
            switch (str.charAt(i)) {
                case '(':
                    arr[0] += 1;
                    break;
                case ')':
                    arr[1] += 1;
                    break;
                case '[':
                    arr[2] += 1;
                    break;
                case ']':
                    arr[3] += 1;
                    break;
                case '{':
                    arr[4] += 1;
                    break;
                default:
                    arr[5] += 1;
            }
        }

        boolean ist = true;
        for (int i = 0; i < arr.length; i += 2) {
            if (arr[i] - arr[i + 1] != 0) {
                ist = false;
                break;
            }
        }
        return ist;
    }


    public static boolean questoin2(String str) {
        if (str.length() % 2 != 0) {
            return false;
        }
        String[] as = {"(", ")", "[", "]", "{", "}"};
        boolean ist = true;
        for (int i = 0; i < as.length; i += 2) {
            String str1 = str.replace(as[i], "");
            int a = str.length() - str1.length();
            String str2 = str1.replace(as[i + 1], "");
            int b = str1.length() - str2.length();
            if (a != b) {
                ist = false;
                break;
            }
            str1 = null;
            str2 = null;
        }

        System.out.println(ist ? "是" : "不是");
        return ist;
    }


    /**
     * 集合 ： 线性存储结构
     * List 接口 线性存储结构
     * Vector
     * Stack 栈 :
     * Set 接口  不可重复 ?
     * <p>
     * Map 接口 k,v
     */

    public static boolean question2(String str) {
        // 容器 即集合 jdk1.5 java要求  集合要声明其存储类型
        Stack<Character> s = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            char t = str.charAt(i);
            if(s.empty()){
                s.push(t);
            }else{
                if(t - 1 == s.peek() || t-2 == s.peek()){
                    s.pop();
                }else{
                    s.push(t);
                }
            }
        }
        return s.empty();
    }

    public static void main(String[] args) {
//        int[] as = {3, 2, 1};
//        int sum1 = 0;
//        for (int i = as.length - 1; i >= 0; i--) {
//            sum1 += as[as.length-i-1] * Math.pow(10, i);
//        }
//        System.out.println(sum1);

        long x = -123;
        long sum = intReverse(x);
        System.out.println(sum);

        System.out.println(question2("[(){}]"));

    }


}
